package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.PatientBean;
import com.cloud.service.FileService;
import com.cloud.service.MyPatientService;
import com.cloud.service.PatientService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-12-5
 * @version 1.0 
 */
@Controller
@RequestMapping("/patient")
public class PatientServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(PatientServlet.class);
	@Autowired
	private PatientService PatientService;
	@Autowired
	private FileService FileService;
	@Autowired
	private MyPatientService MyPatientService;
	
	/**
	 * 查询患者信息
	 * @param request
	 * @param patient
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, PatientBean patient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(patient == null || (CommUtil.isEmpty(patient.getId()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始查询患者[%s]信息", patient.getId()));
		List<PatientBean> retPatientList = new ArrayList<PatientBean>();
		retPatientList = PatientService.getPatientList(patient);
		Map<String, String> _map = new HashMap<String, String>();
		if(retPatientList != null && retPatientList.size() == 1){
			PatientBean _patient = retPatientList.get(0);
			_map.put("id", _patient.getId());
			_map.put("userName", _patient.getUserName());
			_map.put("fullName", _patient.getFullname());
			_map.put("head", FileService.getVisitUrl(_patient.getCrc()));
			_map.put("dic_id", _patient.getDic_id());
			_map.put("gender", _patient.getGender());
			_map.put("birthday", _patient.getBirthday());
			_map.put("idcard", _patient.getIdcard());
			_map.put("proId", _patient.getPro_id());
			_map.put("proName", _patient.getPro_name());
			_map.put("joinDate", _patient.getJoin_date());
			_map.put("createTime", _patient.getCreateTime());
			_map.put("hos_id", _patient.getHos_id());
			if(!"-1".equals(_patient.getPro_id())){
				_map.put("hos_id", MyPatientService.getHosByPatient(_patient));
			}
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", _map);
		return map;
	}
	
	/**
	 * 修改患者信息
	 * @param request
	 * @param patient
	 * @return
	 */
	@RequestMapping(value = "/information/update", method = { RequestMethod.POST})
	public @ResponseBody Object pupload(HttpServletRequest request, @RequestBody PatientBean patient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(patient == null || (CommUtil.isEmpty(patient.getId()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始修改患者[%s]信息", patient.getId()));
		try {
			PatientService.updatePatient(patient);
		} catch (Exception e) {
			logger.error("修改患者信息失败.", e);
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
